package edu.its.dreamteam.web.pages.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.annotations.Retain;
import org.apache.tapestry.beaneditor.BeanModel;
import org.apache.tapestry.ioc.annotations.Inject;
import org.apache.tapestry.services.BeanModelSource;

import edu.its.dreamteam.daf.beans.Club;
import edu.its.dreamteam.daf.dao.IClubDao;
import edu.its.dreamteam.web.base.SecureBasePage;

public class ClubGrid extends SecureBasePage{
	@Inject
	private IClubDao clubDao;
	private List<Club> clubList;
	private Club club;	
	
	@Retain
	private BeanModel clubModel;
	@Inject
	private BeanModelSource clubModelSource;
    @Inject
    private ComponentResources resources;
    
    private boolean delete;
    private List<String> deletedList;
    
    
    public IClubDao getClubDao() {
		return clubDao;
	}
    
	public Club getClub() {
		return club;
	}
	public void setClub(Club club) {
		this.club = club;
	}
	
	public boolean isDelete() {
		return delete;
	}    
	
	public void setDelete(boolean delete) {
		if(delete){			
			getDeletedList().add(getClub().getIdClub());
		}
		
	}	
	public List<String> getDeletedList() {
		if(deletedList == null){
			deletedList = new ArrayList<String>();				
		}
		return deletedList;
	}
	
	public List<Club> getClubList() {
		if(clubList == null){
			clubList = new ArrayList<Club>();	
			clubList = getClubDao().queryForList("", "");
		}
		
		return clubList;
	}
	
	public BeanModel getClubModel() { 
		return clubModel; 
	}
	
	void pageLoaded(){
    	clubModel = clubModelSource.create(Club.class, false, resources);    	
    	clubModel.add("actions", null).label("Edit");
    }	
	
	Object onSuccessFromClubForm(){
		for (String element : getDeletedList()) {
			setClub((Club) getClubDao().findByPrimaryKey(element));
			getClubDao().delete(getClub());
									
		}	
		deletedList = null;
		return ClubGrid.class;
	}
}
